/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ASAInst;

import ASAExpr.ASAExpr;
import exception.DivisionByZeroException;
import exception.VariableNonDeclareeException;

/**
 *
 * @author FM & KB
 */
public class ASAInstIf extends ASAInst {

    private ASAExpr test;
    private ASAInst alors;
    private ASAInst sinon;

    public ASAInstIf(ASAExpr test, ASAInst alors, ASAInst sinon) {
        this.test = test;
        this.alors = alors;
        this.sinon = sinon;
    }

    public ASAInstIf(ASAExpr test, ASAInst alors) {
        this.test = test;
        this.alors = alors;
        this.sinon = null;
    }

    public void executer()  throws VariableNonDeclareeException, DivisionByZeroException{
        if (test.evalue() != 0) {
            this.alors.executer();
        } else {
            if (this.sinon != null) {
                this.sinon.executer();
            }
        }
    }

    public String notationExplorateur(String prefixe) {
        String res = prefixe + getLabelExplorateur() + "\n";
        String prefixeSuiv = prefixe + "   ";

        if (test != null) {
            res += test.notationExplorateur(prefixeSuiv);
            if(this.alors != null){
                res += this.alors.notationExplorateur(prefixeSuiv);
            }
            if(this.sinon != null){
                res += prefixe + "ELSE\n"; 
                res += this.sinon.notationExplorateur(prefixeSuiv);
            }
        } else {
            res += prefixeSuiv + "VIDE\n";
        }
        return res;
    }
    
    public void setElse(ASAInst instElse){
        this.sinon = instElse;
    }

    @Override
    public String getLabelExplorateur() {
        return "IF";
    }
}
